package at.ac.univie.zsu.aguataplan.service;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import at.ac.univie.zsu.aguataplan.dao.ClubDao;
import at.ac.univie.zsu.aguataplan.dao.Dao;
import at.ac.univie.zsu.aguataplan.dao.DetectedEventDao;
import at.ac.univie.zsu.aguataplan.dao.EventDao;
import at.ac.univie.zsu.aguataplan.dao.GameDao;
import at.ac.univie.zsu.aguataplan.dao.TeamDao;
import at.ac.univie.zsu.aguataplan.domain.DetectedEvent;
import at.ac.univie.zsu.aguataplan.domain.Event;
import at.ac.univie.zsu.aguataplan.domain.EventAttribute;
import at.ac.univie.zsu.aguataplan.domain.EventAttributeValueString;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;
import at.ac.univie.zsu.aguataplan.exception.ExportException;
import at.ac.univie.zsu.aguataplan.property.SystemProperties;
import at.ac.univie.zsu.aguataplan.util.Constants;
import au.com.bytecode.opencsv.CSVWriter;

/**
 * @author gerry
 * 
 */
@Service
public class ExportManagerBean implements ExportManager, Constants {
	private static Logger log = Logger.getLogger(ExportManagerBean.class);

	@Autowired
	private EventDao eventDao;

	@Autowired
	private GameDao gameDao;

	@Autowired
	private TeamDao teamDao;

	@Autowired
	private ClubDao clubDao;

	@Autowired
	private DetectedEventDao detectedEventDao;

	@Autowired
	private Dao dao;

	@Autowired
	private SystemProperties systemProperties;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public ExportManagerBean() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	@Override
	public void exportEvent() throws DatabaseException, ExportException {
		List<Event> list;

		try {
			list = eventDao.getEventListNonEmpty();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}

		CSVWriter writer;
		try {
			writer = new CSVWriter(new FileWriter(systemProperties.getDirExport() + EXPORT_EVENT_CSV), ';');
			writer.writeNext(Event.toStringExportHeadline());
			for (Event e : list) {
				List<EventAttribute> eas = eventDao.getEventAttributeListByEventNonEmpty(e);
				for (EventAttribute ea : eas) {
					List<EventAttributeValueString> eavss = eventDao
							.getEventAttributeValueStringListByEventAttribute(ea);
					for (EventAttributeValueString eavs : eavss) {
						writer.writeNext(new String[] { e.toStringExport(), ea.toStringExport(), eavs.toStringExport() });
					}
				}
			}
			writer.close();

		} catch (IOException e) {
			log.error(e.getMessage());
			throw new ExportException("could not export entries", e.getMessage());
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new ExportException("could not export entries", e.getMessage());
		}

	}

	@Override
	public void exportDetectedEvent() throws DatabaseException, ExportException {

		List<DetectedEvent> list = null;

		try {
			list = detectedEventDao.getDetectedEventListOrderByVideoAndDetection();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}

		CSVWriter writer;
		try {
			writer = new CSVWriter(new FileWriter(systemProperties.getDirExport() + EXPORT_DETECTEDEVENT_CSV), ';');
			writer.writeNext(DetectedEvent.toStringExportHeadline());
			for (DetectedEvent de : list) {
				writer.writeNext(de.toStringExport());
			}
			writer.close();

		} catch (IOException e) {
			log.error(e.getMessage());
			throw new ExportException("could not export entries", e.getMessage());
		}

	}

	@Override
	public void dumpDatabase() throws DatabaseException {
		// TODO Auto-generated method stub

	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public ClubDao getClubDao() {
		return clubDao;
	}

	public GameDao getGameDao() {
		return gameDao;
	}

	public void setGameDao(GameDao gameDao) {
		this.gameDao = gameDao;
	}

	public void setClubDao(ClubDao clubDao) {
		this.clubDao = clubDao;
	}

	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

	public TeamDao getTeamDao() {
		return teamDao;
	}

	public void setTeamDao(TeamDao teamDao) {
		this.teamDao = teamDao;
	}

	public EventDao getEventDao() {
		return eventDao;
	}

	public void setEventDao(EventDao eventDao) {
		this.eventDao = eventDao;
	}

	public DetectedEventDao getDetectedEventDao() {
		return detectedEventDao;
	}

	public void setDetectedEventDao(DetectedEventDao detectedEventDao) {
		this.detectedEventDao = detectedEventDao;
	}

	public SystemProperties getSystemProperties() {
		return systemProperties;
	}

	public void setSystemProperties(SystemProperties systemProperties) {
		this.systemProperties = systemProperties;
	}

}
